<?php

namespace app\admin\model;

use think\Model;

class Communityinfo extends Model
{
    // 表名
    protected $name = 'communityinfo';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    
    // 追加属性
    protected $append = [
        'status_text',
        'teaching_point_text',
        'admin_user_text'
    ];
    
    // 状态列表
    public function getStatusList()
    {
        return ['0' => '失效', '1' => '正常'];
    }
    
    // 获取状态文本
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    
    // 关联教学点表
    public function teachingPoint()
    {
        return $this->belongsTo('Ocation', 'teaching_point_id');
    }
    
    // 获取教学点名称
    public function getTeachingPointTextAttr($value, $data)
    {
        $teachingPointId = $data['teaching_point_id'] ?? 0;
        if ($teachingPointId) {
            $teachingPoint = model('ocation')->get($teachingPointId);
            return $teachingPoint ? $teachingPoint['name'] : '';
        }
        return '';
    }
    
    // 关联管理员用户表
    public function adminUser()
    {
        return $this->belongsTo('Admin', 'admin_user_id');
    }
    
    // 获取管理员用户名称
    public function getAdminUserTextAttr($value, $data)
    {
        $adminUserId = $data['admin_user_id'] ?? 0;
        if ($adminUserId) {
            $adminUser = model('Admin')->get($adminUserId);
            return $adminUser ? $adminUser['nickname'] : '';
        }
        return '';
    }
}